#include "pagebypass.h"
#include "ui_pagebypass.h"
#include <QDebug>

PageBypass::PageBypass(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageBypass)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);
    m_lBypassName << "受电弓使能旁路开关"
                << "蓄电池隔离开关"
                << "ATP隔离开关"
                << "门旁路开关"
                << "警惕按钮旁路开关"
                << "总风欠压旁路开关"
                << "停放制动缓解旁路开关"
                << "所有制动缓解监控旁路开关"
                << "障碍物脱轨探测旁路开关"
                << "头灯旁路开关"
                << "备用模式开关"
                << "半车模式开关"
                << "紧急制动旁路接触器";
    InitBypass();
}

PageBypass::~PageBypass()
{
    delete ui;
}

void PageBypass::InitBypass()
{
    m_nAreaWidth = this->width();
    m_nAreaHeight = (this->height()*4)/5;
    m_nTopSpace = 100;
    m_nBottomSpace = 50;
    m_nHSpace = 60;
    m_nNameLabelLen = 300;
    m_nStatLabelLen = 50;
    m_nLabelHeight = 40;

    uint bypassCnt = m_lBypassName.length();
    uint cols = bypassCnt%ROWSINCOL?(bypassCnt/ROWSINCOL+1):(bypassCnt/ROWSINCOL);
    m_nSideSpace = (m_nAreaWidth - cols*(m_nNameLabelLen+2*m_nStatLabelLen)-(cols-1)*m_nHSpace)/2;
    m_nStartx = m_nSideSpace;
    m_nStarty = m_nTopSpace;

    for(uint i=0;i<bypassCnt;i++)
    {
        uint nCol = i/ROWSINCOL;
        if(i%ROWSINCOL == 0)
        {
            QLabel *title1 = new QLabel(this);
            title1->setStyleSheet(StatLabelStyle);
            title1->setGeometry(m_nStartx+nCol*(m_nNameLabelLen+m_nStatLabelLen*2+m_nHSpace),
                               m_nStarty,
                               m_nNameLabelLen,m_nLabelHeight);
            title1->setText("旁路名称");

            QLabel *title2 = new QLabel(this);
            title2->setStyleSheet(StatLabelStyle);
            title2->setGeometry(m_nStartx+m_nNameLabelLen+nCol*(m_nNameLabelLen+m_nStatLabelLen*2+m_nHSpace),
                               m_nStarty,
                               m_nStatLabelLen,m_nLabelHeight);
            title2->setText("Tc1");

            QLabel *title3 = new QLabel(this);
            title3->setStyleSheet(StatLabelStyle);
            title3->setGeometry(m_nStartx+m_nNameLabelLen+m_nStatLabelLen+nCol*(m_nNameLabelLen+m_nStatLabelLen*2+m_nHSpace),
                               m_nStarty,
                               m_nStatLabelLen,m_nLabelHeight);
            title3->setText("Tc2");
        }
        QLabel *nameLabel = new QLabel(this);
        nameLabel->setStyleSheet(StatLabelStyle);
        nameLabel->setGeometry(m_nStartx+nCol*(m_nNameLabelLen+m_nStatLabelLen*2+m_nHSpace),
                           m_nStarty+(i%ROWSINCOL+1)*m_nLabelHeight,
                           m_nNameLabelLen,m_nLabelHeight);
        nameLabel->setText(m_lBypassName.at(i));

        QList<QLabel *> labellist;
        QLabel *stat1 = new QLabel(this);
        stat1->setStyleSheet(LabelWhiteStyle);
        stat1->setGeometry(m_nStartx+m_nNameLabelLen+nCol*(m_nNameLabelLen+m_nStatLabelLen*2+m_nHSpace),
                           m_nStarty+(i%ROWSINCOL+1)*m_nLabelHeight,
                           m_nStatLabelLen,m_nLabelHeight);
        stat1->setAlignment(Qt::AlignCenter);
        stat1->setText("?");
        labellist.append(stat1);
        m_lBypassStat.append(0);

        QLabel *stat2 = new QLabel(this);
        stat2->setStyleSheet(LabelWhiteStyle);
        stat2->setGeometry(m_nStartx+m_nNameLabelLen+m_nStatLabelLen+nCol*(m_nNameLabelLen+m_nStatLabelLen*2+m_nHSpace),
                           m_nStarty+(i%ROWSINCOL+1)*m_nLabelHeight,
                           m_nStatLabelLen,m_nLabelHeight);
        stat2->setAlignment(Qt::AlignCenter);
        stat2->setText("?");
        labellist.append(stat2);
        m_lBypassStat.append(0);
        m_lBypass.append(labellist);
    }
}

void PageBypass::setBypassStat(int bypass, int index, int stat)
{
    if(bypass >= ByPassCnt)
        return;
    if(index >= m_lBypass[bypass].length())
        return;

    m_lBypass[bypass][index]->setText("");
    switch (stat) {
    case 0:
        m_lBypass[bypass][index]->setText("?");
        m_lBypass[bypass][index]->setStyleSheet(LabelWhiteStyle);
        break;
    case 1:
        m_lBypass[bypass][index]->setStyleSheet(LabelYellowStyle);
        break;
    case 2:
        m_lBypass[bypass][index]->setStyleSheet(StatLabelStyle);
        break;
    default:
        m_lBypass[bypass][index]->setStyleSheet(StatLabelStyle);
        break;

    }
    m_lBypassStat[bypass*2+index] = stat;
    if(m_lBypassStat.contains(1))
        emit signal_one_bypass_act(true);
    else
        emit signal_one_bypass_act(false);
}

void PageBypass::on_Btn_Return_clicked()
{
    emit signal_go_back();
}
